package com.lantosec.hmrp.modules.archives.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.archives.entity.UserHouse;
import com.lantosec.hmrp.modules.archives.mapper.UserHouseMapper;
import com.lantosec.hmrp.modules.archives.service.IUserHouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.archives.vo.UserHouseDetailVO;
import com.lantosec.hmrp.modules.archives.vo.UserHouseVO;
import com.lantosec.hmrp.modules.system.service.ISysDictionariesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 领导干部本人、配偶及共同生活的子女住房情况表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-06-12
 */
@Service
@Primary
@Log4j2
public class UserHouseServiceImpl extends ServiceImpl<UserHouseMapper, UserHouse> implements IUserHouseService {
    @Autowired
    private ISysDictionariesService iSysDictionariesService;

    @Override
    @Transactional
    public void saveUserHouse(UserHouseVO vo) {
        UserHouse userHouse = vo.convertToEntity();
        userHouse.setUserHouseId(UUIDUtils.get());
        //获取当前用户id和名称
        userHouse.setCreateId(TokenUtil.getUserId());
        userHouse.setCreateUser(TokenUtil.getName());
        userHouse.setCreateTime(DateUtils.nowDate());
        userHouse.setVerifyStatus(1);
        this.save(userHouse);
        //新增房屋情况类型
        saveUserHouseDetail(vo,userHouse.getUserHouseId());
    }

    /**
     * 新增房屋情况类型
     * @param vo
     */
    private void saveUserHouseDetail(UserHouseVO vo,String userHouseId) {
        //新增房屋情况类型信息
        List<UserHouseDetailVO> userHouseDetailVO =  vo.getUserHouseDetailVO();
        userHouseDetailVO.forEach(param->{
            param.setUserHouseDetailId(UUIDUtils.get());
            param.setUserHouseId(userHouseId);
            baseMapper.saveUserHouseDetail(param);
        });
    }

    @Override
    @Transactional
    public void updateUserHouse(UserHouseVO vo) {
        String userHouseId = vo.getUserHouseId();
        //新增一条数据
        //saveUserHouse(vo);
        //修改这条数据的审核状态为2
        if (StringUtils.isNotEmpty(userHouseId)) {
            UserHouse userHouse = vo.convertToEntity();
            userHouse.setVerifyStatus(1);
            this.updateById(userHouse);
            //新增房屋情况类型信息，先删后增
            baseMapper.deleteUserHouseDetail(userHouseId);
            saveUserHouseDetail(vo,userHouseId);
        }else {
            saveUserHouse(vo);
        }
    }

    @Override
    public List<UserHouseVO> getUserHouse(String userId) {
        List<UserHouseVO> userHouseVOList = new ArrayList<>();
        List<UserHouse> userHouseList = this.list(new QueryWrapper<UserHouse>().eq(UserHouse.ColName.user_id.toString(),userId).orderByDesc(UserHouse.ColName.create_time.toString()));
        userHouseList.forEach(param->{
            UserHouseVO userHouseVO = getUserHouseVo(param);
            List<UserHouseDetailVO> userHouseDetailVO = baseMapper.getUserHouseDetail(param.getUserHouseId());
            userHouseVO.setUserHouseDetailVO(userHouseDetailVO);
            userHouseVOList.add(userHouseVO);
        });
        return userHouseVOList;
    }

    /**
     * 组装领导干部本人、配偶及共同生活的子女住房情况
     * @param param
     */
    private UserHouseVO getUserHouseVo(UserHouse param) {
        UserHouseVO userHouseVO = param.convertToVO();
        try {
            //查询字典表的家庭成员称谓
            String familyAppellation = param.getFamilyAppellation();
            String familyAppellationName = iSysDictionariesService.getById(familyAppellation).getDictionariesShowName();
            userHouseVO.setFamilyAppellationName(familyAppellationName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userHouseVO;
    }

    @Override
    public List<UserHouseVO> getUserHouseVerify(String userId) {
        List<UserHouseVO> userHouseVOList = new ArrayList<>();
        List<UserHouse> userHouseList = this.list(new QueryWrapper<UserHouse>().eq(UserHouse.ColName.user_id.toString(),userId).in(UserHouse.ColName.verify_status.toString(),0,2));
        userHouseList.forEach(param->{
            UserHouseVO userHouseVO = getUserHouseVo(param);
            userHouseVOList.add(userHouseVO);
        });
        return userHouseVOList;
    }

    @Override
    public void verifyUserHouse(UserHouseVO vo) {
        UserHouse userHouse = vo.convertToEntity();
        Integer verifyStatus = userHouse.getVerifyStatus();
        //如果审核不通过
        if (vo.getState() == 0) {
            //审核状态为审核中的变成 已审核
            if (verifyStatus == 2) {
                userHouse.setVerifyStatus(1);
            }
            //审核状态为未审核的变成 审核不通过
            if (verifyStatus == 0) {
                userHouse.setVerifyStatus(3);
            }
        }
        //如果审核通过
        if (vo.getState() == 1) {
            //审核状态为审核中的变为 历史记录
            if (verifyStatus == 2) {
                userHouse.setVerifyStatus(4);
            }
            //审核状态为未审核的变为 已审核
            if (verifyStatus == 0) {
                userHouse.setVerifyStatus(1);
            }
        }
        //获取当前用户id和名称
        userHouse.setVerifyId(TokenUtil.getUserId());
        userHouse.setVerifyUser(TokenUtil.getName());
        userHouse.setVerifyTime(DateUtils.nowDate());
        this.updateById(userHouse);
    }

    @Override
    public List<UserHouseVO> getUserHouseHistory(String userId) {
        List<UserHouseVO> userHouseVOList = new ArrayList<>();
        List<UserHouse> userHouseList = this.list(new QueryWrapper<UserHouse>().eq(UserHouse.ColName.user_id.toString(),userId).eq(UserHouse.ColName.verify_status.toString(),4));
        userHouseList.forEach(param->{
            UserHouseVO userHouseVO = getUserHouseVo(param);
            userHouseVOList.add(userHouseVO);
        });
        return userHouseVOList;
    }
}
